package com.example.javaairportflightanalysis.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import com.example.javaairportflightanalysis.dao.UserMapper;
import com.example.javaairportflightanalysis.dao.UserRoleMapper;
import com.example.javaairportflightanalysis.domin.dto.UserDto;
import com.example.javaairportflightanalysis.domin.userpojo.User;
import com.example.javaairportflightanalysis.exception.MyException;
import com.example.javaairportflightanalysis.exception.MyExceptionEnum;
import com.example.javaairportflightanalysis.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * (User)表服务实现类
 *
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;
    @Resource
    UserRoleMapper userRoleMapper;
    @Override
    public User login(String username, String password) {
        User login = userMapper.selectByUsername(username);
        if (login == null) {
            throw new MyException(MyExceptionEnum.NON_EXISTENT_USER);
        }
        String dbPassword = login.getPassword();
        boolean checkpw = BCrypt.checkpw(password,dbPassword);
        if (!checkpw) {
            throw new MyException(MyExceptionEnum.PASSWORD_ERROR);
        }
        login.setPassword("");
        return login;
    }

    @Override
    public void register(UserDto userDto) {
        String username = userDto.getUsername();
        //查询用户名是否重名
        User result = userMapper.selectByUsername(username);
        if (result!=null){
            throw new MyException(MyExceptionEnum.NAME_EXISTED);
        }
        User user = new User();
        BeanUtils.copyProperties(userDto,user);

        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDate = now.format(dtf);
        user.setCreateTime(formattedDate);
        user.setUpdateTime(formattedDate);
        user.setPassword(BCrypt.hashpw(user.getPassword(),BCrypt.gensalt(10)));
        user.setRemark("用户");
        System.out.println(user);

        //写入数据库
        int count = userMapper.insertSelective(user);
        User user1 = userMapper.selectByUsername(user.getUsername());
        Integer userId = user1.getId();
        userRoleMapper.insertSelective(userId,4);
        if (count==0){
            throw new MyException(MyExceptionEnum.INSERT_FAILED);
        }
    }

    @Override
    public List<User> findAllUser() {
        return userMapper.findAllUser();
    }

    @Override
    public void addUser(User user) {
        userMapper.addUser(user);
    }

    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public void updateUserImg(String path, Integer id) {
        userMapper.updateUserImg(path,id);
    }
}
